// index.js
// 获取应用实例
const app = getApp()
var host = app.globalData.host;
Page({
    data: {

        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        canIUseGetUserProfile: false,
        canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
        // images: ['../../images/haibao/1.jpg', '../../images/haibao/2.jpg', '../../images/haibao/3.jpg'],
        // indicatorDots: true,
        vertical: false,
        autoplay: true,
        circular: true,
        interval: 2000,
        swiperCurrent: 0,
        duration: 500,
        imgUrls: ''
    },
    swiperChange: function(e) {
        // console.log(e)
        this.setData({
            swiperCurrent: e.detail.current
        })
    },
    // 跳转到搜索页面
    // searchInput: function(e) {
    //     wx.navigateTo({
    //         url: '../search/search'
    //     })
    // },
    more: function(e) {
        var id = e.currentTarget.id;
        wx.navigateTo({
            url: '../goods/goods?id=' + id,
        });
    },
    methods: {

        onDot(e) {
            this.setData({
                swiperCurrent: e.currentTarget.dataset.i
            })
            console.log(e)
        },

    },
    // properties: {
    //     selected: {
    //         type: Number,
    //         default: 0
    //     }
    // },
    // 事件处理函数
    bindViewTap() {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    onLoad() {
        this.getBannerList();
        this.getBookList();
        // if (wx.getUserProfile) {
        //     this.setData({
        //         canIUseGetUserProfile: true
        //     })
        // }

    },
    // getBannerList: function() {
    //     var page = this;
    //     wx.request({
    //         url: host + '/api/banner/getBannerList?type=0',
    //         method: 'GET',
    //         data: {},
    //         header: {
    //             'Content-Type': 'application/json'
    //         },
    //         success: function(res) {
    //             console.log(res)
    //             var code = res.data.code;
    //             var list = res.data.data;
    //             if (code == '0000') {
    //                 var imgUrls = new Array();
    //                 for (var i = 0; i < list.lenth; i++) {
    //                     imgUrl.push(host + list[i].url);
    //                 }
    //                 // page.setData({ imgUrl: imgUrls });
    //                 page.setData({ images: imgUrls });

    //             }
    //         }
    //     })
    // },
    getBannerList: function() {
        var page = this;
        wx.request({
            url: host + '/api/banner/getBannerList?type=0',
            method: 'GET',
            data: {},
            header: {
                'Content-Type': 'application/json'
            },
            success: function(res) {
                console.log(res)
                var code = res.data.code;
                var list = res.data.data;
                console.log(list)
                if (code == '0000') {
                    var imgUrls = new Array();
                    for (var i = 0; i < list.length; i++) {
                        imgUrls.push(host + list[i].url);
                    }
                    page.setData({ imgUrls: imgUrls });

                }
            }
        })
    },
    getBookList: function() {
        var page = this;
        wx.request({
            url: host + '/api/goods/getHomeGoodsList',
            method: 'GET',
            data: {},
            header: {
                'Content-Type': 'application/json'
            },
            success: function(res) {
                console.log(res)
                var book = res.data.data;
                wx.setStorageSync('book', book);
                var hotList = book.rmjs;
                var spikeList = book.mssk;
                var bestSellerList = book.cxsj;
                page.setData({
                    hotList: hotList,
                    spikeList: spikeList,
                    bestSellerList: bestSellerList

                });
            }
        })
    },
    getUserProfile(e) {
        // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
        wx.getUserProfile({
            desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                console.log(res)
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
            }
        })
    },
    getUserInfo(e) {
        // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
        console.log(e)
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },
    seeDetail: function(e) {
        // console.log(e)
        var id = e.currentTarget.id;
        //   console.log(id)
        wx.navigateTo({
            url: '../goodsDetail/goodsDetail?goodsId=' + id
        })
    }
})